home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / crystal / cvspk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-12  |  4.6 KB  |  236 lines

  1. /* cvspk.c
  2.  *************************************************************************/
  3.  
  4. #include    <stdio.h>
  5. #include    <string.h>
  6. #include    <ctype.h>
  7.  
  8. #include    "cvmsg.h"
  9. #include    "cvobj.h"
  10. #include    "cvlocs.h"
  11. #include    "cvocab.h"
  12.  
  13. #define    TRUE    (1)
  14. #define    FALSE    (0)
  15.  
  16. extern struct hint hints[];
  17. extern struct cvocab vtab[];
  18. extern char *rmsg[] ;
  19. extern char *mmsg[] ;
  20. extern struct cmsg ctext[] ;
  21. extern int blklin, mltcmd, samvrb ;
  22.  
  23. extern void bug();
  24. extern void exit();
  25.  
  26. char code[] = { 'B'|0x80, 'a'|0x80, 'r'|0x80, 'n'|0x80};
  27.  
  28. void
  29. encode(msg) register char *msg;
  30. {    register int i;
  31.     for (i=0; msg[i]; i++) {
  32.         msg[i] ^= code[i & 3];
  33.     }
  34. }
  35.  
  36. void
  37. putcode(msg) register char *msg;
  38. {    register int i;
  39.     register char c;
  40.     if (*msg & 0x80) {
  41.         for (i=0; c=msg[i]; i++) {
  42.             putchar(c ^ (code[i & 3]));
  43.         }
  44.     } else {
  45.         (void) fputs(msg,stdout);
  46.     }
  47. }
  48.  
  49. void
  50. rspeak(num) register int num;
  51. {    register char *msg ;
  52.     msg = rmsg[num] ;
  53.     if (num == 54) blklin = FALSE ;
  54.     if (*msg == '*') {
  55.         ++msg ;
  56.         mltcmd = FALSE ;
  57.     }
  58.     putcode(msg);
  59. }
  60.  
  61. void
  62. mspeak(num) register int num;
  63. {    (void) fputs(mmsg[num],stdout);
  64. }
  65.  
  66. void
  67. pspeak(obj,prop)
  68.     register struct cvobj *obj;
  69.     register int prop;
  70. {    register char *msg;
  71.     register int i;
  72.  
  73.     msg = obj->props;
  74.     for (i=0; i<prop; ++i) {
  75.         while (*(msg++)) {;}
  76.     }
  77.  
  78.     putcode(msg);
  79. }
  80.  
  81.  
  82. int
  83. yes(quest,agree,refuse) register int quest, agree, refuse;
  84. {
  85.     auto char answer[8] ;
  86.     int i;
  87.     
  88.     for (;;)
  89.     {
  90.         if (quest) rspeak(quest);
  91.         if (fgets(answer,6,stdin) == NULL) { *answer = '\0'; }
  92.         if (!strlen(answer) || answer[strlen(answer)-1] != '\n') {
  93.             while (getchar() != '\n')
  94.                 ;
  95.             answer[0] = '\n'; /* these are error cases */
  96.         }
  97.             
  98.         for (i=0;i<5;++i) {
  99.             if (answer[i] == '\n') {answer[i] = '\0'; break;}
  100.             else answer[i] = tolower(answer[i]);
  101.         }
  102.         if (!strcmp("y",answer) || !strcmp("yes",answer))
  103.         {    if (agree) rspeak(agree);
  104.             return(TRUE);
  105.         }
  106.     
  107.         if (!strcmp("no",answer) || !strcmp("n",answer))
  108.         {    if (refuse) rspeak(refuse);
  109.             return(FALSE);
  110.         }
  111.     
  112.         (void) fputs("\nPlease answer the question.\n",stdout);
  113.     }
  114. }
  115.  
  116. static char cchar = '\0';
  117. static char *tmpchr;
  118.  
  119. /* routine to get the character pointed-to by tmpchr.
  120.  * result to cchar
  121.  * and mltcmd turned off if space-comma or space-period.
  122.  */
  123. static void
  124. gchar()
  125. {    static char lchar;
  126.     lchar = cchar;
  127.     cchar = *(tmpchr++) ;
  128.     if ((lchar == ' ')
  129.         && ((cchar == '.') || (cchar == ','))
  130.         )
  131.     {    mltcmd = FALSE ;
  132.         cchar = '\0' ;
  133.     }
  134. }
  135.  
  136. /* gather a word, put terminator in cchar. */
  137. static char *
  138. gather()
  139. {    register char *r;
  140.  
  141.     do {gchar();} while (cchar == ' ') ;
  142.     if ((cchar == '\0') || (cchar == '.') || (cchar == ',')) return (NULL) ;
  143.     r = tmpchr;
  144.  
  145.     for (;;)
  146.     {    gchar();
  147.         if ((cchar == '\0')
  148.             ||(cchar == '.')
  149.             ||(cchar == ',')
  150.             ||(cchar == ' ') )
  151.         {    *(tmpchr-1) = '\0' ;
  152.             return(--r);
  153.         }
  154.     }
  155. }
  156.  
  157. void
  158. getin()
  159. {    extern char *word1, *word2;
  160.  
  161.     static char ca1[81], ca2[81];
  162.  
  163.     register char *cp2, *cp1;
  164.  
  165.     for (;;mltcmd = FALSE)
  166.     {    word1 = word2 = NULL ;
  167.         if (mltcmd) {
  168.             if ((cchar != '.') && (cchar != ',')) continue ;
  169.             samvrb = (cchar == ',') ;
  170.         } else {
  171.             if (blklin) fputs("\n",stdout);
  172.             if (fgets(ca1,81,stdin) == NULL)
  173.             {    fputs("\nBut, but, ... okay, goodbye.\n",stdout);
  174.                 exit(1);
  175.             }
  176.             for (cp1=ca1; *cp1 != '\0'; ++cp1)
  177.                 *cp1 = (char) tolower((int)*cp1) ;
  178.             if (*(cp1-1) != '\n')
  179.             {    puts("\nThat input was too long... no more than 80\
  180.  characters, please!");
  181.                 while (getchar() !='\n')
  182.                     ;
  183.                 continue; /* this will get us a new input */
  184.             }
  185.             *(cp1-1) = '\0' ; /* alter the final newline */
  186.             mltcmd = TRUE ;
  187.             if ( strcmp(ca1,"again")
  188.                 && strcmp(ca1,"repeat")
  189.                 && strcmp(ca1,"ditto")
  190.                 && strcmp(ca1,"\"")
  191.                 && strcmp(ca1,"''") )
  192.             {    cp1=ca1, cp2=ca2;    /* set to preserve new command */
  193.             } else {
  194.                 cp1=ca2, cp2=ca1;    /* set to repeat old command */
  195.             }
  196.             while ((*(cp2++) = *(cp1++)) != '\0');    /* end AFTER the null */
  197.             tmpchr = ca1 ;
  198.             samvrb = FALSE ;
  199.         }
  200. /* now have the input in 'ca1' */
  201. /* and tmpchr points to the next char to see */
  202.     
  203.         if ((word1 = gather()) == NULL) continue ;
  204.         if (cchar == ' ') word2 = gather();
  205.         return ;
  206.     } /* end of for (implied mltcmd = FALSE) */
  207. }
  208.  
  209. int
  210. vocab(word,sect) register char *word;
  211.     int sect;
  212. {    register int matched = 0, len;
  213.     register struct cvocab *curv = vtab, *likely = NULL ;
  214.  
  215.     len = strlen(word);
  216.  
  217.     if (sect >= 0)
  218.     {    for ( ; curv->cvcode != 0; ++curv)
  219.         {    if (curv->cvcode / 1000 == sect) break ;
  220.         }
  221.     }
  222.  
  223.     for ( ; curv->cvcode != 0; ++curv)
  224.     {    if ((sect >= 0) && ((curv->cvcode / 1000) > sect)) break ;
  225.         ;
  226.         if (!strncmp(word, curv->cvword, len))
  227.         {    ++matched;
  228.             likely = curv ;
  229.             if (strlen(curv->cvword) == len) return(curv->cvcode) ;
  230.         }
  231.     }
  232.  
  233.     if (matched == 1) return(likely->cvcode) ;
  234.     else return(-1);
  235. }
  236.